﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Security.Principal;
using System.Diagnostics;

namespace ALinq.BBS.DataAccess
{
    public enum NodeType
    {
        Root,
        Board,
        Category,
        Forum,
    }

    public partial class Board
    {
        public Forums forums;
        private Boards children;
        private int? level;
        private Board category;

        public Reply AddReply(string title, string content, string author, int postID)
        {
            var reply = new Reply();
            return reply;
        }

        public static Board CreateRootInstance()
        {
            var db = new Database();
            return db.Boards.ToArray().Single(o => o.ID == Const.BoardRootID);
        }

        public Forums Forums
        {
            get
            {
                if (forums == null)
                    forums = new Forums(this);

                return forums;
            }
        }

        //public Boards Children
        //{
        //    get
        //    {
        //        if (children == null)
        //            children = new Boards(this);

        //        return children;
        //    }
        //}


        //public static Board Find(Expression<Func<Board, bool>> func)
        //{
        //    var db = new Database();
        //    return db.Boards.SingleOrDefault(func);
        //}

        //public Post FindPost(Expression<Func<Post, bool>> func)
        //{
        //    return DataContext.Posts.FirstOrDefault(func);
        //}

        //public bool IsRoot()
        //{
        //    return ID == Const.BoardRootID;
        //}

        //public int Level
        //{
        //    get
        //    {
        //        if (level == null)
        //        {
        //            if (Parent == null)
        //                level = 0;
        //            else
        //                level = Parent.Level + 1;
        //        }
        //        return level.Value;
        //    }
        //}

        //public Board Category
        //{
        //    get
        //    {
        //        if (Level <= 2)
        //            return null;

        //        if (category == null)
        //        {
        //            category = Parent;
        //            while (category != null && category.Level != 2)
        //            {
        //                category = category.Parent;
        //            }
        //        }

        //        Debug.Assert(category != null);
        //        Debug.Assert(category.Type == NodeType.Category);

        //        return category;
        //    }
        //}

        //public NodeType Type
        //{
        //    get
        //    {
        //        switch (Level)
        //        {
        //            case 0:
        //                return NodeType.Root;
        //            case 1:
        //                return NodeType.Board;
        //            case 2:
        //                return NodeType.Category;
        //            default:
        //                return NodeType.Forum;
        //        }
        //    }
        //}

    }
}
